package com.example.redis.moduel.service.impl;

import com.example.redis.moduel.entity.User;
import com.example.redis.moduel.mapper.UserMapper;
import com.example.redis.moduel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Optional;

@Service
public class UserServiceImpl implements UserService {
       @Autowired
       private UserMapper userMapper;

       @Autowired
       private RedisTemplate redisTemplate;

       @Override
       public void testRedis() {
              Optional optional = userMapper.findById(1L);
              User user = ( User ) optional.get();
              int a=1;
              int b=0;
              int c=a/b;
              redisTemplate.opsForSet().add(user.getName(), user.getAge());
       }

       @Override
       public String getRedis() {
              /*移动某条数据到指定数据库 存在就不追加 不存在就追加*/
              // redisTemplate.move("ada",3);
              /*得到指定key的链表结构  不从redis中删除元数据*/
              LinkedHashSet zz = ( LinkedHashSet ) redisTemplate.opsForSet().members("ada");
              Iterator ss = zz.iterator();
              while ( ss.hasNext() ) {
                     String mm = ( String ) ss.next();
                     System.out.println(mm);
              }
              /*获得某条数据  该数据会被从redis中删除*/
              /*Object rr = redisTemplate.opsForSet().pop("ada");*/

              redisTemplate.rename("ada", "fuck");
              Object object = redisTemplate.randomKey();
              redisTemplate.renameIfAbsent("fuck", "哈哈哈");
              redisTemplate.persist("哈哈哈");

              /*redisTemplate.restore("boot", "good".getBytes(), 100000, TimeUnit.MINUTES);*/
              return null;
       }
}
